//////////////////////////////
//AIRBuzz.
//Main application factory class.
//////////////////////////////
package org.bbdesign.airbuzz {
	import fl.controls.*;
	import fl.data.*;
	import fl.events.ListEvent;
	//////////////////////////////
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	//Tweener classes.
	import caurina.transitions.Tweener;

	public class Application extends Sprite  {
		//////////////////////////////
		//Internal class variables.
		//////////////////////////////
		//Instance.
		private var _return_buzz_self:Httpserv;
		private var _return_buzz_activity:Httpserv;
		//
		private var _user_txt:String;
		private var _user_id:String;
		private var _user_url:String;
		private var _user_name:String;
		private var _activity_updated:String;
		private var _activity_title:String;
		//
		private var _user_thumb:Loader;
		//
		private var _buzz_pb:ProgressBar;
		//
		private var _user_name_txt:TextField;
		private var _act_cap_txt:TextField;
		private var _invalid_user_txt:TextField;
		private var _activity_body_txt:TextField;
		//
		private var _activitylist:List;
		//
		private var _userImageFrame_mc:DisplayObject;
		//
		private var _act_image_box:Sprite;
		private var _act_body_box:Sprite;
		private var _activity_image:Sprite;
		//Custom class instances.
		private var _text_interface:Interface = new Interface();
		private var _frame_interface:Interface = new Interface();
		private var _btn_load_interface:Interface = new Interface();
		private var _btn_clear_interface:Interface = new Interface();
		private var _pb_loadbuzz_interface:Interface = new Interface();
		private var _activitylist_interface:Interface = new Interface();
		private var _actimage_interface:Interface = new Interface();
		//
		private var _utilty:Utility = new Utility();
		//Google Buzz, private developer API key goes here.
		private var _google_key:String = '';
		//////////////////////////////
		//Constructor.
		//////////////////////////////
		public function Application(swidth:Number, sheight:Number) { // <-- Begin - Constructor.

			//////////////////////////////
			//Init Tweener overwrite.
			//////////////////////////////
			Tweener.autoOverwrite = false;
			//////////////////////////////

			//////////////////////////////
			//Application focus handling.
			//////////////////////////////
			var focushandler:Focushandler = new Focushandler();
			addChild(focushandler);
			//////////////////////////////
			//////////////////////////////

			//////////////////////////////
			//////////////////////////////
			//Application interface.
			//////////////////////////////
			//////////////////////////////

			//////////////////////////////
			//Frame/matte.
			//////////////////////////////
			var frame:Sprite = _frame_interface.drawMatte(1, 0x8AA6BF, 0x4F6273, 8, 11, (swidth - 65), (sheight - 55), 'rect', undefined, undefined, 0.9);
			addChild(frame);
			//////////////////////////////

			//////////////////////////////
			//Updater.
			//////////////////////////////
			var updater:Updater = new Updater();
			addChild(updater);
			//////////////////////////////

			//////////////////////////////
			//Background/layout/icons.
			//////////////////////////////
			//Left bar.
			var buzzBg_mc:DisplayObject = new BuzzLogoContainer();
			frame.addChild(buzzBg_mc);
			with (buzzBg_mc) {
				x = 60;
				y = 235;
			}

			//Large Buzz icon.
			var largebuzz_ico:DisplayObject = new BuzzLargeMC();
			addChild(largebuzz_ico);
			with (largebuzz_ico) {
				x = 68;
				y = 40;
			}

			//User image frame.
			_userImageFrame_mc = new BuzzIcoBox();
			//////////////////////////////

			//////////////////////////////
			//Textfields.
			//////////////////////////////
			//Display user name.
			_user_name_txt = _text_interface.drawTextfield(Fontlib.FontinSansBold,0,0,0xFFFFFF,20,150,100,17,10,false,0x0A6F9B,false,0x54CCBF,'dynamic','center',false,'_user_name_txt', true);
			addChild(_user_name_txt)
			with(_user_name_txt) {
				alpha = 0;
			}

			//Input user name.
			var user_name_input:TextField = _text_interface.drawTextfield(Fontlib.FontinSansRegular,0,0,0xFFFFFF,260,20,150,18,12,true,0x4F6273,true,0x8AA6BF,'input','left',true,'user_name_input', true, false, false, false);
			var user_name_input_label:TextField = _text_interface.drawTextfield(Fontlib.FontinSansBold,0,0,0xFFFFFF,155,22,100,18,12,false,0x4F6273,false,0x8AA6BF,'dynamic','left',false,'user_name_input_label', true, false, false, false);
			user_name_input_label.text = 'User Name or ID: ';
			addChild(user_name_input);
			addChild(user_name_input_label);
			//Event handler.
			user_name_input.addEventListener (KeyboardEvent.KEY_UP, onUserNameInputKeyUp, false, 0, true);
			//Enable/disable font embedding depending on character input.
			var glyphutil:Utility = new Utility();
			function onUserNameInputKeyUp(kbe:KeyboardEvent):void {
				if (kbe.target.length >= 2) {
					//Enable load/clear buttons.
					if (load_btn.enabled == false) {
						Tweener.addTween(load_btn, {alpha:1, time:1, onComplete:btnEnableDisable, onCompleteParams:[load_btn, 'en']});
						Tweener.addTween(clear_btn, {alpha:1, time:1, onComplete:btnEnableDisable, onCompleteParams:[clear_btn, 'en']});
						//Enable/disable font embedding depending on character input.
						glyphutil.checkFontGlyphs(user_name_input, kbe.target.text, 'FontinSansRegular');
					}
					//Place inputted user name into variable.
					_user_txt = kbe.target.text;
				} else if(kbe.target.length <= 2) {
					//Disable load/clear buttons.
					if (load_btn.enabled == true) {
						Tweener.addTween(load_btn, {alpha:0, time:1, onComplete:btnEnableDisable, onCompleteParams:[load_btn, 'ds']});
						Tweener.addTween(clear_btn, {alpha:0, time:1, onComplete:btnEnableDisable, onCompleteParams:[clear_btn, 'ds']});
						//Enable/disable font embedding depending on character input.
						glyphutil.checkFontGlyphs(user_name_input, kbe.target.text, 'FontinSansRegular');
					}
				}
			}

			//Activity feed caption.
			_act_cap_txt = _text_interface.drawTextfield(Fontlib.FontinBold,0,0,0xFFFFFF,155,55,170,22,20,false,0x0A6F9B,false,0x54CCBF,'dynamic','center',false,'_act_cap_txt', true);
			_act_cap_txt.text = 'buzz#activityFeed';
			addChild(_act_cap_txt);

			//Invalid user.
			_invalid_user_txt = _text_interface.drawTextfield(Fontlib.FontinSansBold,0,0,0xEC0000,190,230,240,22,14,false,0x0A6F9B,false,0x54CCBF,'dynamic','center',false,'_invalid_user_txt', true);
			_invalid_user_txt.text = '';
			addChild(_invalid_user_txt);

			//////////////////////////////

			//////////////////////////////
			//Buttons.
			//////////////////////////////
			//Load buzz data button.
			var load_btn:Button = _btn_load_interface.drawButton(Fontlib.FontinBold, 0x260505, 12, 'Load Buzz', 29, 450, 80, false);
			addChild(load_btn);
			load_btn.addEventListener(MouseEvent.MOUSE_UP, loadBuzzMouseUpHandler, false, 0, false);
			with (load_btn) {
				alpha = 0.9;
				enabled = false;
			}
			//Clear buzz data button.
			var clear_btn:Button = _btn_clear_interface.drawButton(Fontlib.FontinBold, 0x260505, 12, 'Clear Buzz', 29, 480, 80, false);
			addChild(clear_btn);
			clear_btn.addEventListener(MouseEvent.MOUSE_UP, clearBuzzMouseUpHandler, false, 0, false);
			with (clear_btn) {
				alpha = 0.9;
				enabled = false;
			}
			//////////////////////////////

			//////////////////////////////
			//Load/Manipulate Data.
			//////////////////////////////

			//////////////////////////////
			//Event handling.
			//////////////////////////////

			//////////////////////////////
			//MouseUp handler/Clear buzz data.
			function clearBuzzMouseUpHandler(e:MouseEvent):void {
				//Remove any extant user image objects.
				if (getChildByName('_user_thumb') != null) {
					Tweener.addTween(_userImageFrame_mc, {delay:0.2, time:1, alpha:0, onComplete:remObj, onCompleteParams:[_userImageFrame_mc]});
					Tweener.addTween(_user_name_txt, {delay:0.2, time:1, alpha:0, onComplete: clrTxt, onCompleteParams:[_user_name_txt, 'html']});
					Tweener.addTween(_user_thumb, {delay:0.2, time:1, alpha:0, onComplete:remObj, onCompleteParams:[_user_thumb]});
				}

				//Disable load/clear buttons.
				if (load_btn.enabled == true) {
					Tweener.addTween(load_btn, {alpha:0, time:1, onComplete:btnEnableDisable, onCompleteParams:[load_btn, 'ds']});
					Tweener.addTween(clear_btn, {alpha:0, time:1, onComplete:btnEnableDisable, onCompleteParams:[clear_btn, 'ds']});
				}

				//Clear invalid user message, if any.
				clrTxt(_invalid_user_txt, 'text');

				//Clear user name text field.
				clrTxt(user_name_input, 'text');

				//Cleanup main buzz data objects.
				remObj(_return_buzz_self);
				remObj(_return_buzz_activity);

				//Cleanup activity list.
				if(_activitylist != null) {
					Tweener.addTween(_activitylist, {delay:0.2, time:1, alpha:0, onComplete:remObj, onCompleteParams:[_activitylist]});
				}

				//Cleanup activity image objects.
				if(_activity_image != null) {
					Tweener.addTween(_activity_image, {delay:0.2, time:1, alpha:0, onComplete:remObj, onCompleteParams:[_activity_image]});
				}
				if(_act_image_box != null) {
					Tweener.addTween(_act_image_box, {delay:0.2, time:1, alpha:0, onComplete:remObj, onCompleteParams:[_act_image_box]});
				}

				//Cleanup activity body text objects.
				if(_act_body_box != null) {
					Tweener.addTween(_act_body_box, {delay:0.2, time:1, alpha:0, onComplete:remObj, onCompleteParams:[_act_body_box]});
				}

			}

			//////////////////////////////
			//MouseUp handler/Load buzz data.
			function loadBuzzMouseUpHandler(e:MouseEvent):void {

				//Get public user data. JSON format.
				//Append necessary Google API key.
				_return_buzz_self = new Httpserv('https://www.googleapis.com/buzz/v1/people/' + _user_txt + '/@self?key=' + _google_key + '&alt=json', 'POST', 'text');
				_return_buzz_self.addEventListener('HTTPSERV_OUTER_DATASENT_COMPLETE', returnBuzzUserDataIsLoaded);

				//Error handling.
				_return_buzz_self.addEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzUserDataError);
				addChild(_return_buzz_self);

				//Get public user activity data. JSON format.
				//Append necessary Google API key.
				_return_buzz_activity = new Httpserv('https://www.googleapis.com/buzz/v1/activities/' + _user_txt + '/@public?key=' + _google_key + '&alt=json', 'POST', 'text');
				_return_buzz_activity.addEventListener('HTTPSERV_OUTER_DATASENT_COMPLETE', returnBuzzActivityDataIsLoaded);

				//Error handling.
				_return_buzz_activity.addEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzActivityDataError);
				addChild(_return_buzz_activity);

				//Json parse error handling.
				_return_buzz_self.addEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);
				_return_buzz_activity.addEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);

				//Add progress bar.
				_buzz_pb = _pb_loadbuzz_interface.buildProgressBar(_return_buzz_activity, "Loading Buzz...", 345, 60, Fontlib.FontinSansRegular, 0xffffff, 11);
				addChild(_buzz_pb);
				with(_buzz_pb) {
					alpha = 0;
				}
				Tweener.addTween(_buzz_pb, {alpha:1, time:1});
			}

			//////////////////////////////
			//Manipulate user activity data.
			//Buzz activity data is loaded.
			//////////////////////////////
			function returnBuzzActivityDataIsLoaded(e:Event):void {
				//Clear listeners.
				_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_COMPLETE', returnBuzzActivityDataIsLoaded);
				_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzActivityDataError);

				//Create data provider for activity list.
				var datp:DataProvider = new DataProvider();

				//Activity items.
				//Last updated.
				_activity_updated = _return_buzz_activity.resultcontainer.act_updated;
				//Main title.
				_activity_title = _return_buzz_activity.resultcontainer.act_title;
				for (var a_prop:String in _return_buzz_activity.resultcontainer.act_items) {
					//Activity title.
					var activity_content_title:String = _return_buzz_activity.resultcontainer.act_items[a_prop].title;
					//Activity published.
					var activity_content_pub:String = _return_buzz_activity.resultcontainer.act_items[a_prop].published;
					//Activity updated.
					var activity_content_up:String = _return_buzz_activity.resultcontainer.act_items[a_prop].updated;
					//Activity content/note.
					var activity_content_note:String = _return_buzz_activity.resultcontainer.act_items[a_prop].object.content;
					//Image/photo attachments.
					var attachments:Object = _return_buzz_activity.resultcontainer.act_items[a_prop].object.attachments;
					if(attachments != null) {
						for (var b_prop:String in attachments) {
							for (var c_prop:String in attachments[b_prop].links) {
								for (var d_prop:String in attachments[b_prop].links.enclosure) {
									var activity_img_href:String = attachments[b_prop].links.enclosure[d_prop].href;
									var activity_img_width:String = attachments[b_prop].links.enclosure[d_prop].width;
									var activity_img_height:String = attachments[b_prop].links.enclosure[d_prop].height;
									break;
								}
								break;
							}
							break;
						}
						//Add items to activity list data provider + images.
						datp.addItem({
							label:activity_content_title,
							published:activity_content_pub,
							updated:activity_content_up,
							content:activity_content_note,
							img:activity_img_href,
							w:activity_img_width,
							h:activity_img_height
						});
					} else {
						//Add items to activity list data provider - images.
						datp.addItem({
							label:activity_content_title,
							published:activity_content_pub,
							updated:activity_content_up,
							content:activity_content_note
						});
					}
				}

				//Remove old activity list.
				remObj(_activitylist);
				//Create new activity list.
				_activitylist = _activitylist_interface.buildList(datp);
				//Determine image icons.
				_activitylist.iconFunction = determineIcon;
				function determineIcon(item:Object):Class {
					return (item.img != null) ? BuzzSmallMC : null;
				}
				//Add to display list.
				addChild(_activitylist);
				with (_activitylist) {
					rowHeight = 25;
					alpha = 0;
					x = 160;
					y = 90;
					width = 240;
					height = 310;
				}
				//Tween.
				Tweener.addTween(_activitylist, {time:1, delay:0.2, alpha:1 } );

				//On list item click.
				_activitylist.addEventListener(ListEvent.ITEM_CLICK, listItemClickHandler);
				function listItemClickHandler(e:ListEvent):void {

					//Load any images associated with activity.
					if (e.item.img != undefined) {

						//Remove old activity image.
						if(_activity_image != null) {
							Tweener.addTween(_activity_image, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_activity_image]});
						}

						//Load image.
						var act_image:Loader = _utilty.urlLoaderBuilder(e.item.img, 'text', 'loader')
						act_image.contentLoaderInfo.addEventListener(Event.COMPLETE, activityImageLoadedHandler);
						function activityImageLoadedHandler(e:Event): void {

							//Remove old activity image background.
							if(_act_image_box != null) {
								Tweener.addTween(_act_image_box, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_act_image_box]});
							}
							//Add new activity image background.
							_act_image_box = _actimage_interface.drawMatte(1, 0x8AA6BF, 0x4F6273, 160, 440, 170, 130, 'rect', undefined, undefined, 0, true);
							addChild(_act_image_box);

							//Add new activity image.
							var actbm:Bitmap = new Bitmap(((act_image.content as Bitmap).bitmapData as BitmapData).clone());
							actbm.smoothing = true;
							act_image.unload();
							_activity_image = new Sprite();
							_activity_image.addChild(actbm);

							addChild(_activity_image);
							with(_activity_image) {
								name = '_activity_image';
								width = (_activity_image.width < 160) ? _activity_image.width : 160;
								height = (_activity_image.height < 120) ? _activity_image.height : 120;
								alpha = 0;
								x = 165;
								y = 445;
							}

							//Calculate appropriate image dimensions.
							var imgwidth:Number = (actbm.width > stage.width - 50) ? (stage.width / 1.5) : actbm.width;
							var imgheight:Number = (actbm.height > stage.height - 50) ? (stage.height / 1.5) : actbm.height;

							//Activity image click/rollover/rollout.
							_activity_image.addEventListener(MouseEvent.CLICK, activityImageClickHandler, false, 0, false);
							_activity_image.addEventListener(MouseEvent.ROLL_OVER, activityImageRollOverHandler, false, 0, false);
							_activity_image.addEventListener(MouseEvent.ROLL_OUT, activityImageRollOutHandler, false, 0, false);
							//Click state.
							var clicked:Boolean = false;
							//Click handler.
							function activityImageClickHandler(e:MouseEvent):void {
								switch(clicked) {
									case false:
										Tweener.addTween(_activity_image, {time:0.75, alpha:0});
										Tweener.addTween(_act_image_box, {time:0.75, alpha:0});
										Tweener.addTween(_activity_image, {delay:1, width:imgwidth, height:imgheight, x:30, y:30});
										Tweener.addTween(_act_image_box, {delay:1, width:imgwidth + 20, height:imgheight + 20, x:20, y:20});
										Tweener.addTween(_activity_image, {time:2, delay:1.8, alpha:1});
										Tweener.addTween(_act_image_box, {time:2, delay:1.8, alpha:1});
										clicked = true;
									break;
									case true:
										Tweener.addTween(_activity_image, {time:0.75, alpha:0});
										Tweener.addTween(_act_image_box, {time:0.75, alpha:0});
										Tweener.addTween(_activity_image, {delay:1,width:160, height:120, x:165, y:445});
										Tweener.addTween(_act_image_box, {delay:1,width:170, height:130, x:160, y:440});
										Tweener.addTween(_activity_image, {time:2, delay:1.8, alpha:1});
										Tweener.addTween(_act_image_box, {time:2, delay:1.8, alpha:1});
										clicked = false;
									break;
									default:
										clicked = false;
								}
							}
							//Rollover handler.
							function activityImageRollOverHandler(e:MouseEvent):void {
								buttonMode = true;
								useHandCursor = true;
							}
							//Rollout handler.
							function activityImageRollOutHandler(e:MouseEvent):void {
								buttonMode = false;
								useHandCursor = false;
							}

							//Animate activity image objects.
							Tweener.addTween(_activity_image, {time:1, delay:0.2, alpha:1});
							Tweener.addTween(_act_image_box, {time:1, delay:0.2, alpha:1});
						}
					} else {
						//Cleanup activity image objects, if any.
						if(_activity_image != null) {
							Tweener.addTween(_activity_image, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_activity_image]});
						}
						if(_act_image_box != null) {
							Tweener.addTween(_act_image_box, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_act_image_box]});
						}
					}

					//Load activity body content into textfield.
					//Remove old activity body text background.
					if(_act_body_box != null) {
						Tweener.addTween(_act_body_box, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_act_body_box]});
					}
					//Add new activity body text background.
					_act_body_box = _actimage_interface.drawMatte(1, 0x8AA6BF, 0x4F6273, 430, 90, 180, 310, 'rect', undefined, undefined, 0, true);
					addChild(_act_body_box);

					//Activity body text holder.
					_activity_body_txt = _text_interface.drawTextfield("_sans",0,0,0xffffff,10,10,170,300,12,false,0x8AA6BF,false,0x4F6273,'dynamic','left',false,'_activity_body_txt', true);
					_activity_body_txt.htmlText = e.item.content + '<br /><br />Published: ' + e.item.published + '<br />Updated: ' + e.item.updated;
					_act_body_box.addChild(_activity_body_txt);

					//Animate activity image objects.
					Tweener.addTween(_act_body_box, {time:1, delay:0.2, alpha:1});

				}
			}

			//////////////////////////////
			//Manipulate user data.
			//Buzz user data is loaded.
			//////////////////////////////
			function returnBuzzUserDataIsLoaded(e:Event):void {
				//Clear listeners.
				_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_COMPLETE', returnBuzzUserDataIsLoaded);
				_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzUserDataError);

				//Clear invalid user message, if any.
				clrTxt(_invalid_user_txt, 'text');

				//User profile url.
				_user_url = _return_buzz_self.resultcontainer.user_profileUrl;
				//User name.
				_user_name = _return_buzz_self.resultcontainer.user_displayName;
				_user_name_txt.htmlText = '<a href="' + _user_url + '">' + _user_name + '</a>';
				//User thumbnail.
				if(getChildByName('_user_thumb') == null) {
					_user_thumb = _utilty.urlLoaderBuilder(_return_buzz_self.resultcontainer.user_thumbnailUrl, 'text', 'loader')
					_user_thumb.contentLoaderInfo.addEventListener(Event.COMPLETE, userThumbLoadHandler);
					function userThumbLoadHandler(e:Event):void {
						addChild(_user_thumb);
						with (_user_thumb) {
							name = '_user_thumb';
							alpha = 0;
							scaleX = 0.6;
							scaleY = 0.6;
							x = 40;
							y = 80;
						}
						//User image frame events.
						addChild(_userImageFrame_mc);
						with (_userImageFrame_mc) {
							alpha = 0;
							x = 69;
							y = 109;
						}
						_userImageFrame_mc.addEventListener(MouseEvent.ROLL_OVER, userImageFrameOver, false, 0, false);
						_userImageFrame_mc.addEventListener(MouseEvent.ROLL_OUT, userImageFrameOut, false, 0, false);
						//Roll over.
						function userImageFrameOver():void {
							Tweener.addTween(_userImageFrame_mc, {time:1, alpha:0});
							Tweener.addTween(_user_thumb, {time:1, scaleX:1, scaleY:1, transition:"easeOutElastic"});
						}
						//Roll out.
						function userImageFrameOut():void {
							Tweener.addTween(_userImageFrame_mc, {time:1, alpha:1});
							Tweener.addTween(_user_thumb, {time:1, scaleX:0.6, scaleY:0.6, transition:"easeOutElastic"});
						}

						//Animate user image objects.
						Tweener.addTween(_userImageFrame_mc, {time:1, delay:0.2, alpha:1});
						Tweener.addTween(_user_name_txt, {time:1, delay:0.2, alpha:1});
						Tweener.addTween(_user_thumb, {time:1, delay:0.2, alpha:1});
					}
				}
				//Remove progress bar.
				Tweener.addTween(_buzz_pb, {time:1, alpha:0, onComplete:remObj, onCompleteParams:[_buzz_pb]});
			}
			//////////////////////////////
		} // <-- End - Constructor.

		//////////////////////////////

		//////////////////////////////
		//Private, internal methods.
		//////////////////////////////

		//////////////////////////////

		//////////////////////////////
		//Error Conditions.
		//Handler methods.
		//////////////////////////////
		//Error handling - Activity data.
		private function returnBuzzActivityDataError(e:Event):void {
			//Clear listeners.
			_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzActivityDataError);
			_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzUserDataError);

			_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);
			_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);

			//Remove progress bar.
			if(_buzz_pb != null) {
				Tweener.addTween(_buzz_pb, {time:1, alpha:0, onComplete:remObj, onCompleteParams:[_buzz_pb]});
			}
		}
		//Error handling - User data.
		private function returnBuzzUserDataError(e:Event):void {
			//Clear listeners.
			_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzUserDataError);
			_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR', returnBuzzActivityDataError);

			_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);
			_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);

			//Remove progress bar.
			if(_buzz_pb != null) {
				Tweener.addTween(_buzz_pb, {time:1, alpha:0, onComplete:remObj, onCompleteParams:[_buzz_pb]});
			}
			_invalid_user_txt.text = 'Invalid User!';

			//Remove any extant user image objects.
			if (getChildByName('_user_thumb') != null) {
				Tweener.addTween(_userImageFrame_mc, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_userImageFrame_mc]});
				Tweener.addTween(_user_name_txt, {time:1, delay:0.2, alpha:0, onComplete: clrTxt, onCompleteParams:[_user_name_txt, 'html']});
				Tweener.addTween(_user_thumb, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_user_thumb]});
			}

			//Remove any extant activity list.
			if(_activitylist != null) {
				Tweener.addTween(_activitylist, {time:1, delay:0.2, alpha:1, onComplete:remObj, onCompleteParams:[_activitylist]});
			}

			//Cleanup activity image objects, if any.
			if(_activity_image != null) {
				Tweener.addTween(_activity_image, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_activity_image]});
			}
			if(_act_image_box != null) {
				Tweener.addTween(_act_image_box, {time:1, delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_act_image_box]});
			}

			//Cleanup activity body text objects.
			if(_act_body_box != null) {
				Tweener.addTween(_act_body_box, {delay:0.2, alpha:0, onComplete:remObj, onCompleteParams:[_act_body_box]});
			}
		}
		//Error handling - Json parse error.
		private function returnBuzzJsonError(e:Event):void {
			_return_buzz_activity.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);
			_return_buzz_self.removeEventListener('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', returnBuzzJsonError);
			_invalid_user_txt.text = 'Invalid Data Feed. Load Aborted!';
		}
		//////////////////////////////

		//////////////////////////////
		//Enable/disable buttons.
		//////////////////////////////
		private function btnEnableDisable(btn:Button, state:String):void {
			switch(state) {
				case 'en': {
					btn.enabled = true;
				} break;
				case 'ds': {
					btn.enabled = false;
				} break;
			}
		}
		//////////////////////////////

		//////////////////////////////
		//Clear textfield. Text/html.
		//////////////////////////////
		private function clrTxt(txt:TextField, type:String):void {
			switch(type) {
				case 'text': {
					if (txt.text != '') {
						txt.text = '';
					}
				} break;
				case 'html': {
					if (txt.htmlText != '') {
						txt.htmlText = '';
					}
				} break;
			}
		}
		//////////////////////////////

		//////////////////////////////
		//Remove object.
		//////////////////////////////
		private function remObj(obj:*):void {
			if(obj != null) {
				try {
					var childidx:int = getChildIndex(obj);
					removeChildAt(childidx);
				} catch (error:Error) {
					trace('Error in function: remObj()');
					trace('Error type: ' + error);
				} finally {
					obj = null;
				}
			}
		}
		//////////////////////////////

		//////////////////////////////
	}
}

